package com.benchproject.persist;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;

import com.benchproject.domainmodel.Account;

@Repository("accountDao")
public class AccountDaoImpl extends HibernateDaoSupport implements AccountDao {
	
	
    /**
     * @see com.benchproject.persist.AccountDao#getAccountForCode(String)
     */
    public Account getAccountForId(Long id) {
//          return getAccountForDetatchedCriteria(code);
        List<Account> results = getHibernateTemplate().find("from Account where id = ?", id);
        return results.get(0);
    }

    /**
     * @see com.benchproject.persist.AccountDao#getAllAccounts()
     */
    public List<Account> getAllAccounts() {
        return getHibernateTemplate().loadAll(Account.class);
    }


    /**
     * todo - (Nice to have) Hibernates findByExample way of retrieving object
     */
    public Account getAccountForExample(String code){
        Account searchObj = new Account();
//        searchObj.setName(code);
        List<Account> results = getHibernateTemplate().findByExample(searchObj);        
        return results.get(0);
    }

    /**
     * todo - (Nice to have) Hibernates DetatchedCriteria way of retrieving object
     */
    public Account getAccountForDetatchedCriteria(String code){
        Account searchObj = new Account();
//        searchObj.setCode(code);
//        searchObj.setName("American Airlines");
        Example example = Example.create(searchObj);
//        example.excludeProperty("code");
//        example.excludeNone();
        DetachedCriteria criteria = DetachedCriteria.forClass(Account.class).add(example);
        List<Account> results = getHibernateTemplate().findByCriteria(criteria);
        return results.get(0);
    }

}
